public class EatLock {
    public static void main(String[] args) {
        Object soup = new Object();
        Object dumplings = new Object();

        Thread thread1 = new Thread(()->{
           synchronized (soup) {
               //等待是为了让另一个线程有时间给第一层加锁
               try {
                   Thread.sleep(3000);

               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               synchronized (dumplings) {
                   System.out.println("阿珍喝到了汤，也吃到了饺子");
               }
           }
        },"阿珍");
        thread1.start();
        Thread thread2 = new Thread(()->{
            synchronized (soup) {
                //等待是为了让另一个线程有时间给第一层加锁
                try {
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (dumplings) {
                        System.out.println("阿强吃到了饺子，也喝到了汤");
                }
            }
        },"阿强");
        thread2.start();
    }
}
